package com.xyh.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xyh.common.annotation.RefreshFilterChain;
import com.xyh.common.util.ResultCode;
import com.xyh.common.util.ResultVO;
import com.xyh.system.mapper.RoleOperatorMapper;
import com.xyh.system.model.Menu;
import com.xyh.system.model.Operator;
import com.xyh.system.model.RoleOperator;
import com.xyh.system.service.IOperatorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author kyle
 * @since 2021-01-05
 */
@Controller
@RequestMapping("/operator")
public class OperatorController {

    @Autowired
    private IOperatorService operatorService;
    @Autowired
    private RoleOperatorMapper roleOperatorMapper;

    /**
     * 跳转至operator-list界面
     *
     * @return
     */
    @GetMapping("/index")
    public String toOperatorIndex() {
        return "operator/operator-list";
    }

    /**
     * 根据菜单权限id，获取功能权限列表
     *
     * @param menuId
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    public ResultVO getOperatorList(@RequestParam(required = false) Integer menuId) {
        List<Operator> operators = operatorService.list(new QueryWrapper<Operator>().eq("menu_id", menuId));//mybatis-plus自带方法
        return ResultVO.success(operators);
    }

    /**
     * 获取 菜单功能权限树
     *
     * @return
     */
    @GetMapping("/tree")
    @ResponseBody
    public ResultVO getOperatorTree() {
        List<Menu> menuAndOperatorTree = operatorService.getAllMenuAndOperatorTree();
        return ResultVO.success(menuAndOperatorTree);
    }

    /**
     * 跳转至功能权限新增页面
     *
     * @return
     */
    @GetMapping
    public String toAdd() {
        return "operator/operator-add";
    }

    /**
     * 执行功能权限新增操作节点,并且刷新过滤器链
     *
     * @param operator
     * @return
     */
    @RefreshFilterChain
    @PostMapping
    @ResponseBody
    public ResultVO add(Operator operator) {
        boolean b = operatorService.save(operator);
        if (b) {
            return ResultVO.success();
        } else {
            return ResultVO.fail(ResultCode.SERVER_INNER_ERROR);
        }
    }

    /**
     * 跳转至更新功能权限页面，和新增界面一致
     *
     * @param operatorId
     * @param model
     * @return
     */
    @GetMapping("/{operatorId}")
    public String toUpdate(@PathVariable("operatorId") Integer operatorId, Model model) {
        Operator operator = operatorService.getById(operatorId);//mybatis-plus自带方法
        model.addAttribute("operator", operator);
        return "operator/operator-add";
    }

    /**
     * 执行功能权限修改操作节点,并且刷新过滤器链
     *
     * @param operator
     * @return
     */
    @RefreshFilterChain
    @PutMapping
    @ResponseBody
    public ResultVO update(Operator operator) {
        boolean b = operatorService.updateById(operator);//mybatis-plus自带方法
        if (b) {
            return ResultVO.success();
        } else {
            return ResultVO.fail(ResultCode.SERVER_INNER_ERROR);
        }
    }

    /**
     * 删除功能操作权限
     *
     * @param operatorId
     * @return
     */
    @DeleteMapping("/{operatorId}")
    @ResponseBody
    public ResultVO delete(@PathVariable("operatorId") Integer operatorId) {

        boolean b = operatorService.removeById(operatorId);//mybatis-plus自带方法
        if (b) {
            roleOperatorMapper.delete(new QueryWrapper<RoleOperator>().eq("operator_id", operatorId));
            return ResultVO.success();
        } else {
            return ResultVO.fail(ResultCode.SERVER_INNER_ERROR);
        }
    }
}
